# Definition for a Node.
class Node:
    def __init__(self, val, neighbors):
        self.val = val
        self.neighbors = neighbors


class Solution:
    def cloneGraph(self, node: 'Node') -> 'Node':
        if not node:
            return None
        no_list, res = [node], {}
        while no_list:
            mid = no_list.pop(0)
            if mid.val not in res:
                res[mid.val] = Node(mid.val, [])
            for neighbor in mid.neighbors:
                if neighbor.val not in res:
                    res[neighbor.val] = Node(neighbor.val, [])
                    no_list.append(neighbor)
                res[mid.val].neighbors.append(neighbor)
        return res[node.val]
